//
//  MMGLoadingButton.m
//  MyApp
//
//  Created by Don on 2017/9/5.
//  Copyright © 2017年 com.memego. All rights reserved.
//

#import "MMGLoadingButton.h"



@interface MMGLoadingButton ()
@property (nonatomic, strong)UIActivityIndicatorView *loadingView;
@end
@implementation MMGLoadingButton
- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        
    }
    return self;
}

- (void)startAnimation {
    DLog(@"loading view frame :%@",NSStringFromCGRect(self.bounds));
    [self.loadingView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.mas_top);
        make.center.mas_equalTo(self);
        make.size.mas_equalTo(self.bounds.size);
    }];
    [self.loadingView startAnimating];
}

- (void)stopAnimation {
    [self.loadingView stopAnimating];
}

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    
    if (enabled) {
        [self.loadingView stopAnimating];
    }else {
        [self startAnimation];
    }
    
    
}

#pragma mark - lazy init 
- (UIActivityIndicatorView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[UIActivityIndicatorView alloc] initWithFrame:self.bounds];
        [_loadingView setUserInteractionEnabled:YES];
        [_loadingView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
//        [_loadingView setColor:[UIColor mmg_colorWithHex:kMMGColorHex_MainColor]];
        //CGAffineTransform transform = CGAffineTransformMakeScale(.7f, .7f);
        //_loadingView.transform = transform;
        
        [self addSubview:_loadingView];
    }

    return _loadingView;
}
@end
